﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Runtime;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace OpenSource.Controls
{
    
    [SupportsEventValidation]
    [ToolboxItem(false)]
    internal class InputFormTable : Table
    {
        private int _parentLevel;
        private string _parentID;
        private bool _parentIDSet;

        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        internal InputFormTable()
            : this(1)
        {
        }

        internal InputFormTable(int parentLevel)
        {
            this._parentLevel = parentLevel;
            this._parentIDSet = false;
        }

        internal InputFormTable(string parentID)
        {
            this._parentID = parentID;
            this._parentIDSet = true;
        }

        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            base.AddAttributesToRender(writer);
            string str = this._parentID;
            if (!this._parentIDSet)
                str = this.GetParentID();
            if (str == null)
                return;
            writer.AddAttribute(HtmlTextWriterAttribute.Id, str);
        }

        private string GetParentID()
        {
            if (this.ID != null)
                return (string)null;
            Control control = (Control)this;
            for (int index = 0; index < this._parentLevel; ++index)
            {
                control = control.Parent;
                if (control == null)
                    break;
            }
            if (control != null && !string.IsNullOrEmpty(control.ID))
                return control.ClientID;
            else
                return (string)null;
        }
    }

}
